package com.example.mpandroidchart_all.FragmentChart;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mpandroidchart_all.MainActivity;
import com.example.mpandroidchart_all.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;

import java.util.ArrayList;

public class FragmentCombinedChart extends Fragment {
    private CombinedChart combinedChart;
    private BarChart barChart;
    private BubbleChart bubbleChart;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_combined,container,false);
        combinedChart=view.findViewById(R.id.combined_chart);
        barChart=new BarChart(getContext());
        bubbleChart=new BubbleChart(getContext());

        xAxis();
        setDescription();
        initData();
        return view;
    }
    private void xAxis(){
        XAxis xAxis = combinedChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(2);
        xAxis.setAxisMaximum(14);
        xAxis.setDrawGridLines(false);
    }
    private void setDescription(){
        //bar
        Description description=new Description();
        description.setText("测试柱状图");
        description.setTextSize(20);
        description.setTextColor(Color.RED);
        barChart.setDescription(description);
    }
    private void initData() {
        combinedChart.setExtraOffsets(15f,30f,15f,30f);
        setChartData();
    }
    private void setChartData(){
        ArrayList<BarEntry> values1=new ArrayList<>();
        values1.add(new BarEntry(4,5));
        values1.add(new BarEntry(7,7));
        values1.add(new BarEntry(10,9));

        ArrayList<BarEntry> values2=new ArrayList<>();
        values2.add(new BarEntry(5,2));
        values2.add(new BarEntry(8,7));
        values2.add(new BarEntry(11,8));


        BarDataSet set1=new BarDataSet(values1,"柱状图1");
        set1.setColor(Color.RED);

        BarDataSet set2=new BarDataSet(values2,"柱状图2");
        set1.setColor(Color.BLUE);

        ArrayList<IBarDataSet> dataSets1=new ArrayList<>();
        dataSets1.add(set1);dataSets1.add(set2);

        BarData barData=new BarData(dataSets1);

        //bubble
        ArrayList<BubbleEntry> values3=new ArrayList<>();
        values3.add(new BubbleEntry(3f,3f,0.5f));
        values3.add(new BubbleEntry(4f,4f,0.5f));
        values3.add(new BubbleEntry(6f,6f,2f));
        values3.add(new BubbleEntry(8f,2f,2f));
        values3.add(new BubbleEntry(9f,6f,2f));

        BubbleDataSet set3=new BubbleDataSet(values3,"BubbleChart1");
        set3.setColor(Color.YELLOW);

        ArrayList<IBubbleDataSet> dataSets=new ArrayList<>();
        dataSets.add(set3);

        BubbleData bubbleData=new BubbleData(dataSets);

        XAxis xAxis = bubbleChart.getXAxis();
        xAxis.setAxisMaximum(10);
        xAxis.setAxisMinimum(0);

        CombinedData combinedData=new CombinedData();
        combinedData.setData(barData);
        combinedData.setData(bubbleData);

        combinedChart.setData(combinedData);
        combinedChart.invalidate();
    }
}
